
//
//  NewsViewController.m
//  必争医疗
//
//  Created by 8号球 on 14-11-19.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsTableViewCell.h"
#import "NewsModel.h"
#import "AsyncGetModel.h"
#import "WebViewController.h"
#import "UIImageView+WebCache.h"
#import "NewsScrollViewCell.h"
#import "imageTableViewCell.h"
@interface NewsViewController ()<UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate>

@end

@implementation NewsViewController
- (void)dealloc
{
 
    [_childTable release];
    [_childArray release];
    [_backScroll release];
    [_seg release];
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hotArray = [NSMutableArray array];
        self.hotsmallArray = [NSMutableArray array];
        self.childArray = [NSMutableArray array];
        self.sexArray = [NSMutableArray array];
        self.emotionArray = [NSMutableArray array];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"新闻";
    
    //执行方法
    [self creatBackScroll];
    [self creatSegment];
    [self creatHotTable];
    [self creatChildTable];
    [self creatSexTable];
    [self creatEmotionTable];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.tabBarController.tabBar setHidden:NO];
    [self.navigationController.navigationBar setHidden:YES];
}

#pragma mark -
#pragma mark 创建segment
- (void)creatSegment
{
    NSArray *arr = [NSArray arrayWithObjects:@"热点",@"育儿",@"两性",@"情感",nil];
    self.seg = [[UISegmentedControl alloc] initWithItems:arr];
    [self.seg setFrame:CGRectMake(10, 20, self.view.frame.size.width - 20, 55)];
    [_seg addTarget:self action:@selector(changeBackScroll:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_seg];
    [_seg release];
}

//关联seg和scrollview
- (void)changeBackScroll:(UISegmentedControl *)sender
{
    UISegmentedControl *seg = (UISegmentedControl *)sender;
    [_backScroll setContentOffset:CGPointMake(seg.selectedSegmentIndex * self.view.frame.size.width, 0)];
}

#pragma mark -
#pragma mark 创建背部scrollView
- (void)creatBackScroll
{
    self.backScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20 + 55, self.view.frame.size.width, self.view.frame.size.height - 55 - 20)];

    [self.backScroll setContentSize:CGSizeMake(self.view.frame.size.width * 4, 0)];
    [_backScroll setBackgroundColor:[UIColor cyanColor]];
    [_backScroll setPagingEnabled:YES];
    [_backScroll setBounces:NO];
    [_backScroll setDelegate:self];
    [self.view addSubview:_backScroll];
    [_backScroll release];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint point = _backScroll.contentOffset;
    int i = point.x / 375;
    _seg.selectedSegmentIndex = i;
}


#pragma mark -
#pragma mark 创建热点tableview
- (void)creatHotTable
{
    self.hotTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 55 - 20 - 49) style:UITableViewStylePlain];
    [_hotTable setBackgroundColor:[UIColor purpleColor]];
  
    _hotTable.delegate = self;
    _hotTable.dataSource = self;
    [_backScroll addSubview:_hotTable];
    [_hotTable release];
    
    [self creatHotConnet];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (tableView == _childTable) {
        return [_childArray count];
    } if (tableView == _hotTable) {
        return [_hotArray count];
    } if (tableView == _sexTable) {
        return [_sexArray count];
    } if (tableView == _emotionTable) {
        return [_emotionArray count];
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        if (indexPath.row == 0) {
            return 200;
        }
        return 100;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == _emotionTable) {
        
        if (indexPath.row == 0) {
            static NSString *str = @"bigEmotion";
            imageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
            if (!cell) {
                cell = [[imageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
            }
            
            NewsModel *model = [_emotionArray objectAtIndex:indexPath.row];
            [cell.aimage setImageWithURL:[NSURL URLWithString:model.image]];
            return cell;
        }
        
        static NSString *str = @"emotion";
        NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (!cell) {
            cell = [[NewsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
        }
        NewsModel *model = [_emotionArray objectAtIndex:indexPath.row];
        cell.model =model;
        return cell;
        
    }

    
    
    if (tableView == _sexTable) {
        
        if (indexPath.row == 0) {
            static NSString *str = @"bigSex";
            imageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
            if (!cell) {
                cell = [[imageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
            }
            
            NewsModel *model = [_sexArray objectAtIndex:indexPath.row];
            [cell.aimage setImageWithURL:[NSURL URLWithString:model.image]];
            return cell;
        }
        
        static NSString *str = @"Sex";
        NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (!cell) {
            cell = [[NewsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
        }
        NewsModel *model = [_sexArray objectAtIndex:indexPath.row];
        cell.model =model;
        return cell;
        
    }

    

    if (tableView == _childTable) {
        
        if (indexPath.row == 0) {
            static NSString *str = @"bigChild";
            imageTableViewCell *cell = [_childTable dequeueReusableCellWithIdentifier:str];
            if (!cell) {
                cell = [[imageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
            }
        
            NewsModel *model = [_childArray objectAtIndex:indexPath.row];
            [cell.aimage setImageWithURL:[NSURL URLWithString:model.image]];
            return cell;
        }

        static NSString *str = @"child";
        NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (!cell) {
            cell = [[NewsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
        }
        NewsModel *model = [_childArray objectAtIndex:indexPath.row];
        cell.model =model;
        return cell;
        
    }
    
    if (tableView == _hotTable) {
        if (indexPath.row == 0) {
            static NSString *str = @"big";
            NewsScrollViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
            if (!cell) {
                cell = [[NewsScrollViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
            }
            
            NewsModel *model = [_hotsmallArray objectAtIndex:0];
            NewsModel *modelsecond = [_hotsmallArray objectAtIndex:1];
            NewsModel *modelthird = [_hotsmallArray objectAtIndex:2];
            
            [cell.imagefirst setImageWithURL:[NSURL URLWithString:model.image]];
            UIButton *first = [UIButton buttonWithType:UIButtonTypeCustom];
            [first setFrame:CGRectMake(0, 0, 375, 200)];
            [first addTarget:self action:@selector(firstAction) forControlEvents:   UIControlEventTouchUpInside];
            [cell.imagefirst addSubview:first];
            
            [cell.imageSecond setImageWithURL:[NSURL URLWithString:modelsecond.image]];
            UIButton *second = [UIButton buttonWithType:UIButtonTypeCustom];
            [second setFrame:CGRectMake(0, 0, 375, 200)];
            [second addTarget:self action:@selector(secondAction) forControlEvents:   UIControlEventTouchUpInside];
            [cell.imageSecond addSubview:second];
            
            [cell.imageThird setImageWithURL:[NSURL URLWithString:modelthird.image]];
            UIButton *third = [UIButton buttonWithType:UIButtonTypeCustom];
            [third setFrame:CGRectMake(0, 0, 375, 200)];
            [third addTarget:self action:@selector(thirdAction) forControlEvents:   UIControlEventTouchUpInside];
            [cell.imageThird addSubview:third];
            return cell;
            
        }
        
        static NSString *str = @"small";
        NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (!cell) {
            cell = [[NewsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
        }
        NewsModel *model = [_hotArray objectAtIndex:indexPath.row];
        cell.model = model;
        return cell;

    }
    
    
    return nil;
}


- (void)firstAction
{
    NewsModel *model = [_hotsmallArray objectAtIndex:0];
    WebViewController *web = [[WebViewController alloc] init];
    web.id = model.id;
    [self.navigationController pushViewController:web animated:YES];
}
- (void)secondAction
{
    NewsModel *model = [_hotsmallArray objectAtIndex:1];
    WebViewController *web = [[WebViewController alloc] init];
    web.id = model.id;
    [self.navigationController pushViewController:web animated:YES];
}
- (void)thirdAction
{
    NewsModel *model = [_hotsmallArray objectAtIndex:2];
    WebViewController *web = [[WebViewController alloc] init];
    web.id = model.id;
    [self.navigationController pushViewController:web animated:YES];
}


#pragma mark -
#pragma mark 创建热点网络链接
- (void)creatHotConnet
{
    [AsyncGetModel asyncWithUrl:NewsHotUrl dic:nil block:^(NSMutableData *data) {
        NSMutableArray *bigArr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        for(int i = 0; i < [bigArr count]; i ++) {
            if (i < 3) {
            NewsModel *model = [[NewsModel alloc] init];
            [model setValuesForKeysWithDictionary:[bigArr objectAtIndex:i]];
                [_hotsmallArray addObject:model];
            } else {
            NewsModel *model = [[NewsModel alloc] init];
            [model setValuesForKeysWithDictionary:[bigArr objectAtIndex:i]];
            [self.hotArray addObject:model];
            [_hotTable reloadData];
            }
        }
    }];

    
}

#pragma mark -
#pragma mark 热点页面跳转详情
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NewsModel *model = [[[NewsModel alloc] init] autorelease];
    if (tableView == _hotTable) {
        model  = [_hotArray objectAtIndex:indexPath.row];
    } else if (tableView == _childTable) {
        model  = [_childArray objectAtIndex:indexPath.row];
    } else if (tableView == _sexTable) {
        model  = [_sexArray objectAtIndex:indexPath.row];
    } else if (tableView == _emotionTable) {
        model  = [_emotionArray objectAtIndex:indexPath.row];
    }
    
    WebViewController *web = [[WebViewController alloc] init];
    web.id = model.id;
    [self.navigationController pushViewController:web animated:YES];
}


#pragma mark -
#pragma mark 创建育儿tableview
- (void)creatChildTable
{
    self.childTable = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height - 55 - 20 - 49) style:UITableViewStylePlain];
    [_childTable setBackgroundColor:[UIColor purpleColor]];
    _childTable.delegate = self;
    _childTable.dataSource = self;

    [_backScroll addSubview:_childTable];
    [_childTable release];

    [self creatChildConnet];
}

#pragma mark -
#pragma mark 创建育儿网络链接
- (void)creatChildConnet
{
    [AsyncGetModel asyncWithUrl:NewsChildUrl dic:nil block:^(NSMutableData *data) {
        NSMutableArray *bigArr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        for(int i = 0; i < [bigArr count]; i ++) {
                NewsModel *model = [[NewsModel alloc] init];
                [model setValuesForKeysWithDictionary:[bigArr objectAtIndex:i]];
                [self.childArray addObject:model];
                [_childTable reloadData];
            }
    }];
}


#pragma mark -
#pragma mark 创建两性tableview
- (void)creatSexTable
{
    self.sexTable = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 2, 0, self.view.frame.size.width, self.view.frame.size.height - 55 - 20 - 49) style:UITableViewStylePlain];
    [_sexTable setBackgroundColor:[UIColor purpleColor]];
    _sexTable.delegate = self;
    _sexTable.dataSource = self;
    
    [_backScroll addSubview:_sexTable];
    [_sexTable release];
    
    [self creatSexConnet];
}

#pragma mark -
#pragma mark 创建两性网络链接
- (void)creatSexConnet
{
    [AsyncGetModel asyncWithUrl:NewsSexUrl dic:nil block:^(NSMutableData *data) {
        NSMutableArray *bigArr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        for(int i = 0; i < [bigArr count]; i ++) {
            NewsModel *model = [[NewsModel alloc] init];
            [model setValuesForKeysWithDictionary:[bigArr objectAtIndex:i]];
            [self.sexArray addObject:model];
            [_sexTable reloadData];
        }
    }];
}


#pragma mark -
#pragma mark 创建情感tableview
- (void)creatEmotionTable
{
    self.emotionTable = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 3, 0, self.view.frame.size.width, self.view.frame.size.height - self.view.frame.size.height / 12 - 20 - 49) style:UITableViewStylePlain];
    [_emotionTable setBackgroundColor:[UIColor purpleColor]];
    _emotionTable.delegate = self;
    _emotionTable.dataSource = self;
    
    [_backScroll addSubview:_emotionTable];
    [_emotionTable release];
    
    [self creatEmotionConnet];
}

#pragma mark -
#pragma mark 创建情感网络链接
- (void)creatEmotionConnet
{
    [AsyncGetModel asyncWithUrl:NewsEmotionUrl dic:nil block:^(NSMutableData *data) {
        NSMutableArray *bigArr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        for(int i = 0; i < [bigArr count]; i ++) {
            NewsModel *model = [[NewsModel alloc] init];
            [model setValuesForKeysWithDictionary:[bigArr objectAtIndex:i]];
            [self.emotionArray addObject:model];
            [_emotionTable reloadData];
        }
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
